﻿Namespace SetupNetNode
    Public Class FSetupNetNode
        Private mResult As Boolean = False

        Public Shadows Function ShowDialog(ByVal owner As IWin32Window) As Boolean
            txtHostName.Text = NetNode.Settings.Host
            txtPort.Text = NetNode.Settings.Port.ToString
            txtReconnectInterval.Text = NetNode.Settings.ReconnectInterval.ToString
            txtNickname.Text = NetNode.Settings.Nickname
            txtEmail.Text = NetNode.Settings.Email
            Me.Icon = CommonResources.My.Resources.Buddy

            If NetNode.Settings.SetupLevel_Saved < 1 Then
                rbNetWorkMode_Public.Checked = True
            ElseIf NetNode.Settings.Listen Then
                rbNetWorkMode_Public.Checked = True
            Else
                rbNetWorkMode_Private.Checked = True
            End If
            chkAutoConnect.Checked = NetNode.Settings.AutoConnect

            MyBase.ShowDialog(owner)
            Return mResult
        End Function

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Error_Clear()
            If rbNetWorkMode_Public.Checked Then
                If txtHostName.Text.Trim.Length = 0 Then
                    Error_Set(txtHostName, "Invalid host name!")
                Else
                    Try
                        Dim ip As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(txtHostName.Text)
                    Catch ex As System.Net.Sockets.SocketException
                        Error_Set(txtHostName, ex.Message)
                    End Try
                End If
                If txtPort.Text.Trim.Length = 0 Then
                    Error_Set(txtPort, "Invalid port!")
                Else
                    Dim iPort As Integer = 0
                    If Not Integer.TryParse(txtPort.Text, iPort) Then
                        Error_Set(txtPort, "Invalid port!")
                    Else
                        If iPort < 1 Or iPort > 65534 Then
                            Error_Set(txtPort, "Port must be between 1 and 65534!")
                        ElseIf iPort <> NetNode.Settings.Port Then
                            Try
                                Dim clsListen As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, iPort)
                                clsListen.Start()
                                clsListen.Stop()
                            Catch ex As Exception
                                Error_Set(txtPort, ex.Message)
                            End Try
                        End If
                    End If
                End If
            End If
            Dim iReconnectInterval As Integer = 0

            If Integer.TryParse(txtReconnectInterval.Text, iReconnectInterval) Then
                If iReconnectInterval < 300 Or iReconnectInterval > 30000 Then
                    Error_Set(txtReconnectInterval, "Reconnect interval must be between 300 and 30000!")
                End If
            Else
                Error_Set(txtReconnectInterval, "Invalid reconnect interval!")
            End If

            If txtNickname.Text.Trim.Length = 0 Then
                Error_Set(txtNickname, "Nickname must be set!")
            End If
            If txtEmail.Text.Trim.Length = 0 Then
                Error_Set(txtEmail, "Email must be set!")
            ElseIf Not System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$") Then
                Error_Set(txtEmail, "Invalid email adress!")
            End If

            If Not mAnyError Then
                NetNode.Settings.Listen = rbNetWorkMode_Public.Checked
                If rbNetWorkMode_Public.Checked Then
                    NetNode.Settings.Host = txtHostName.Text.Trim
                    NetNode.Settings.Port = Integer.Parse(txtPort.Text.Trim)
                End If
                NetNode.Settings.ReconnectInterval = Integer.Parse(txtReconnectInterval.Text.Trim)
                NetNode.Settings.Nickname = txtNickname.Text.Trim
                NetNode.Settings.SetupLevel_Saved = NetNode.Settings.SetupLevel_Required
                NetNode.Settings.Email = txtEmail.Text.Trim
                NetNode.Settings.AutoConnect = chkAutoConnect.Checked

                NetNode.Manager.ReevalSettings()

                mResult = True
                Me.Close()
            End If

        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            mResult = False
            Me.Close()
        End Sub

        Private mAnyError As Boolean = False
        Private Sub Error_Clear()
            mAnyError = False
            ErrorProvider1.Clear()
        End Sub
        Private Sub Error_Set(ByVal control As Control, ByVal text As String)
            If Not mAnyError Then
                mAnyError = True
                control.Focus()
            End If
            ErrorProvider1.SetError(control, text)
        End Sub
    End Class
End Namespace